package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constrant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 */

@RequestMapping("/role")
@RestController
public class RoleContorller {
    @Reference
    private RoleService roleService;
    //新增角色
    // @RequestBody 作用:解析提交过来的json数据,
    @RequestMapping("/add")

    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器异常");
        }
        return new Result(true,"新增角色成功");
    }

    //分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return roleService.findPage(queryPageBean);
    }
    //根据id查询角色信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
           Role role = roleService.findById(id);
            return new Result(true,"成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
    //角色id查多个权限id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds= roleService.findPermissionIdsByRoleId(id);

            return new Result(true,"成功",permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }


    }
//角色id查多个菜单id
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleIdInteger (Integer id){
        try {
            List<Integer> menuIds= roleService.findMenuIdsByRoleId(id);

            return new Result(true,"成功",menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }


    }
@RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.edit(role,permissionIds,menuIds);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器异常");
        }

        return new Result(true,"编辑角色成功,欢迎您再次使用");
    }
}
